﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public class GotoErrorArgs : EventArgs
    {
        public string Filename;
        public int Line;
        public string Error;

        public GotoErrorArgs(string f, int l, string e)
        {
            Filename = f;
            Line = l;
            Error = e;
        }
    }

    public partial class Errors : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public delegate void GotoErrorHandler(object sender, GotoErrorArgs e);
        public event GotoErrorHandler GotoError;

        public Errors()
        {
            InitializeComponent();
            listView1.Columns[listView1.Columns.Count - 1].Width = -2;
        }

        public void Clear()
        {
            listView1.Items.Clear();
        }

        public void AddError(string line)
        {
            Regex r = new Regex(@"^(?<Filename>.*?)\((?<Line>[0-9]+)\)(?<Error>.*)$");
            Match m = r.Match(line);
            ListViewItem.ListViewSubItem[] i = {
                new ListViewItem.ListViewSubItem(),
                new ListViewItem.ListViewSubItem(),
                new ListViewItem.ListViewSubItem()};
            i[0].Text = m.Groups["Line"].Value;
            i[1].Tag = m.Groups["Filename"].Value;
            i[1].Text = System.IO.Path.GetFileName(m.Groups["Filename"].Value);
            i[2].Text = m.Groups["Error"].Value.Replace(":error ***", "");
            listView1.Items.Add(new ListViewItem(i,0));
        }

        private void Output_Load(object sender, EventArgs e)
        {

        }

        private void Output_Resize(object sender, EventArgs e)
        {
            listView1.Columns[listView1.Columns.Count - 1].Width = -2;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            var i = listView1.SelectedItems[0];
            if (GotoError != null)
                GotoError(this, new GotoErrorArgs(
                    (string)i.SubItems[1].Tag,
                    int.Parse(i.SubItems[0].Text),
                    i.SubItems[2].Text));

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
